/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Chatree Siangklom
 * Created On : Jan 15, 2013  5:40:18 PM 
 * e-Mail : dr_chatree@hotmail.com
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : jpa.service.synchro.staging
 * File Name : SyncAttachFileServices.java
 * TODO
 */
package jpa.service.synchro.staging;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import jpa.entitys.synchro.common.CommonEntity;
import jpa.entitys.synchro.staging.SyncAttachFile;
import jpa.service.synchro.common.JPAService;

/**
 * @author chatree
 *
 */
public class SyncAttachFileServices extends JPAService {
	
	private static final Logger log = Logger.getLogger(SyncAttachFileServices.class);
	
	/**
	 * @param em
	 */
	public SyncAttachFileServices(EntityManager em) {
		super(em);
	}

	public SyncAttachFile findById(String id) throws Exception {				
		
		return super.ConvertToYoureObject(super.find(id, "SyncAttachFile.findById"));				
	}
	
	public List<SyncAttachFile> findByInstanceNo(String instanceNo) throws Exception {		
		
		return super.ConvertToYoureObject(super.findByInstanceNo(instanceNo, "SyncAttachFile.findByInstanceNo"));				
	}
	
	public SyncAttachFile Updated(SyncAttachFile value) throws Exception {
		try{
			
			if(value != null)
			{
				value.setSyncUpdatedby(this.getUser());
				value.setSyncUpdatedon(this.getCurrent_date());
				return (SyncAttachFile) super.updated(value);
			}else
				return null;
		}catch(Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
			throw e;
		}
	}
	
	public List<SyncAttachFile> Inserted(List<SyncAttachFile> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(SyncAttachFile obj : vlauelist)
			{
				if(CommonService.IsNull(obj.getPromisStagingId()))
				{
					String id = super.SequenceNext("SYNC_ATTACH_FILES");					
					obj.setPromisStagingId(id);								
					obj.setSyncCreatedby(this.getUser());
					obj.setSyncCreatedon(this.getCurrent_date());
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());
					obj.setIsactive("Y");
					obj.setIssync("N");
					common_lst.add(obj);
				}
			}
			
			if(CommonService.getSize(common_lst) > 0)
				super.inserted(common_lst);
			
		}catch(Exception e)
		{
			e.printStackTrace();
			log.error(e.getMessage());
			throw e;
		}
		return vlauelist;
	}
	
	public List<SyncAttachFile> Updated(List<SyncAttachFile> vlauelist) throws Exception {
		try{
			List<CommonEntity> common_lst = new ArrayList<CommonEntity>();			
			for(SyncAttachFile obj : vlauelist)
			{
				if(!CommonService.IsNull(obj.getPromisStagingId()))
				{
					obj.setSyncUpdatedby(this.getUser());
					obj.setSyncUpdatedon(this.getCurrent_date());					
					common_lst.add(obj);					
				}
			}
			
			if(CommonService.getSize(common_lst) > 0)
				super.updated(common_lst);
			
		}catch(Exception e)
		{
			e.printStackTrace();
			log.error(e.getMessage());
			throw e;
		}
		return vlauelist;
	}	

}
